package com.sajus.web.billing.admin.action;

import java.util.Date;
import java.util.List;

import javax.naming.NamingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sajus.billing.client.delegate.BusinessDelegateFactory;
import com.sajus.billing.client.dto.OfferingDTO;
import com.sajus.billing.client.dto.PromotionDTO;
import com.sajus.billing.client.dto.ServiceTypeDTO;

/**
 * Acts as a Struts 2 controller that responds
 * to a user action by setting the value
 * of the Message model class, and returns a String 
 * result.
 * @author Bruce Phillips
 *
 */
public class PromotionAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	/**
	 * The model class that stores the message
	 * to display in the view.
	 */
	private List<PromotionDTO> promotions;
	
	private PromotionDTO promotion;
	
	/*
	 * Creates the MessageStore model object and 
	 * returns success.  The MessageStore model
	 * object will be available to the view.
	 * (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		if ( this.promotion != null ) {
			if ( this.promotion.getId() == null ){
				if ( this.promotion.getDescription() != null ||
						this.promotion.getDuration() != 0 ||
						this.promotion.getEndDate() != null ||
						this.promotion.getName() != null ||
						this.promotion.getStartDate() != null
						){
					
					BusinessDelegateFactory.getInstance().getServiceTypeManager().savePromotion( this.promotion );
				}
			} else {
				BusinessDelegateFactory.getInstance().getServiceTypeManager().savePromotion( this.promotion );
				System.out.println("Persist object Success reset value" );
				this.promotion = new PromotionDTO();
			}
		}
		
		String paramValue = ServletActionContext.getRequest().getParameter("promotionId");
		if ( paramValue != null && !paramValue.equals( "" ) ){
			Integer id = new Integer( Integer.parseInt( paramValue ) );
		
			this.promotion = BusinessDelegateFactory.getInstance().getServiceTypeManager().getPromotion( id );
		}
		
		if( this.promotions == null || this.promotions.isEmpty() ){
			
			this.promotions = BusinessDelegateFactory.getInstance().getServiceTypeManager().getPromotions();
			
			System.out.println(" size of the promotion " + this.promotions.size() );
		}
		
		return SUCCESS;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public List<PromotionDTO> getPromotions() {
		return promotions;
	}


	public void setPromotions(List<PromotionDTO> promotions) {
		this.promotions = promotions;
	}


	public PromotionDTO getPromotion() {
		return promotion;
	}


	public void setPromotion(PromotionDTO promotion) {
		this.promotion = promotion;
	}
	
	public Date getStartDate(){
		if ( this.promotion != null ){
			return this.promotion.getStartDate();
		}
		return null;
	}
	
	public void setStartDate(Date date){
		if ( this.promotion != null ){
			this.promotion.setStartDate( date );
		}
	}
	
	public Date getEndDate(){
		if ( this.promotion != null ){
			return this.promotion.getEndDate();
		}
		return null;
	}
	
	public void setEndDate(Date date){
		if ( this.promotion != null ){
			this.promotion.setEndDate( date );
		}
	}
}
